package com.person.jc.service.security;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.utils.Utils;

public class TsCrudLogService {

	public static final TsCrudLogService me = new TsCrudLogService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer(
				"select c.user_id, c.user_name,\n" +
				"       c.dept_name,\n" + 
				"       c.opt_menu,\n" + 
				"       (select ck.kind_name from ts_code_kind_data ck\n" + 
				"              where c.opt_type = ck.kind_code\n" + 
				"              and ck.type_code = 'opt_type') opt_type,\n" + 
				"       c.opt_detail, " +
				"		(case when c.opt_ip <> '0:0:0:0:0:0:0:1' then c.opt_ip else '127.0.0.1' end) opt_ip ,\n" + 
				"       DATE_FORMAT(c.opt_date,'%Y-%m-%d %T') opt_date\n" + 
				"       from ts_crud_log c,ts_user u\n" + 
				"       where 1=1 and c.user_id = u.user_id\n");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("userId")))){
			sql.append(" and u.user_id like ?");
			params.add("%"+temp+"%");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("userName")))){
			sql.append(" and u.user_name like ?");
			params.add("%"+temp+"%");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("startDate")))){
			sql.append(" and DATE_FORMAT(c.opt_date,'%Y-%m-%d') >= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("endDate")))){
			sql.append(" and DATE_FORMAT(c.opt_date,'%Y-%m-%d') <= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deptId")))){
			sql.append(" and u.dept_id = ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("optType")))){
			sql.append(" and c.opt_type = ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("menuName")))){
			sql.append(" and c.opt_menu like ?");
			params.add("%"+temp+"%");
		}
		sql.append("  order by c.opt_date desc ");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
}
